﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_TuanDuiBiaoService : It_TuanDuiBiaoService
    {
        public readonly MyContext _Context;

        public t_TuanDuiBiaoService(MyContext context)
        {
            _Context = context;
        }

        public int Add(t_TuanDuiBiao t_TuanDuiBiao)
        {
            _Context.t_TuanDuiBiao.Add(t_TuanDuiBiao);
            return _Context.SaveChanges();
        }

        public List<t_RenYuanBiao> GetRenYuanJingLi(int shu)
        {
            return _Context.t_RenYuanBiao.Where(x => x.GangWeiId == shu).ToList();
        }

        public t_TuanDuiBiao GetShuju(t_TuanDuiBiao t_TuanDuiBiao)
        {
            return _Context.t_TuanDuiBiao.FirstOrDefault(x => x.TuanDuiMingCheng == t_TuanDuiBiao.TuanDuiMingCheng && x.JingLiId == t_TuanDuiBiao.JingLiId && x.ZhuangTai == t_TuanDuiBiao.ZhuangTai);
        }

        public t_TuanDuiBiao Getid(int id)
        {
            return _Context.t_TuanDuiBiao.FirstOrDefault(x => x.Id == id);
        }

        public string GetListWithFenYeZheYe(int tuanduiid, int start, int limit, string dir, int column)
        {
            var list = new List<t_TuanDuiRenYuanGuanLianBiao>();
            list = _Context.t_TuanDuiRenYuanGuanLianBiao.Include(x => x.t_RenYuanBiao).Where(x => x.TuanDuiId ==tuanduiid).ToList();
            switch (column)
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public string GetListWithFenYeAnChang(int tuanduiid, int start, int limit, string dir, int column)
        {
            var list = new List<t_TuanDuiAnChangGuanLianBiao>();
            list = _Context.t_TuanDuiAnChangGuanLianBiao.Include(x => x.t_AnChangBiao).Where(x => x.TuanDuiId == tuanduiid).ToList();
            switch (column)
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public bool DelZheYeShangChu(int id) 
        {
            _Context.t_TuanDuiRenYuanGuanLianBiao.Remove(_Context.t_TuanDuiRenYuanGuanLianBiao.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public bool DelAnChangShangChu(int id)
        {
            _Context.t_TuanDuiAnChangGuanLianBiao.Remove(_Context.t_TuanDuiAnChangGuanLianBiao.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public string GetListWithFenYe(t_TuanDuiBiao t_TuanDuiBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_TuanDuiBiao);
            var list = new List<t_TuanDuiBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_TuanDuiBiao.Include(x => x.t_RenYuanBiao_JingLi).Where(returnwhere).ToList();
            }
            else
            {
                list = _Context.t_TuanDuiBiao.Include(x => x.t_RenYuanBiao_JingLi).ToList();
            }
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.TuanDuiMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.TuanDuiMingCheng).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.JingLiId).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.JingLiId).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.ZhuangTai).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.ZhuangTai).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public string ReturnWhere(t_TuanDuiBiao t_TuanDuiBiao)
        {
            string returnstr = "id != 1";
            if (t_TuanDuiBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(" + t_TuanDuiBiao.BeiZhu1 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(" + t_TuanDuiBiao.BeiZhu2 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(" + t_TuanDuiBiao.BeiZhu3 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(" + t_TuanDuiBiao.BeiZhu4 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(" + t_TuanDuiBiao.BeiZhu5 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(" + t_TuanDuiBiao.BeiZhu6 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(" + t_TuanDuiBiao.BeiZhu7 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(" + t_TuanDuiBiao.BeiZhu8 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(" + t_TuanDuiBiao.BeiZhu9 + ")";
            }
            if (t_TuanDuiBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(" + t_TuanDuiBiao.BeiZhu10 + ")";
            }
            if (t_TuanDuiBiao.TuanDuiMingCheng != null && t_TuanDuiBiao.TuanDuiMingCheng != "")
            {
                returnstr = returnstr + " and TuanDuiMingCheng.Contains(\"" + t_TuanDuiBiao.TuanDuiMingCheng + "\")";
            }
            if (t_TuanDuiBiao.JingLiId != 0)
            {
                returnstr = returnstr + " and JingLiId == " + t_TuanDuiBiao.JingLiId + "";
            }
            if (t_TuanDuiBiao.ZhuangTai != "0")
            {
                returnstr = returnstr + " and ZhuangTai.Contains(\"" + t_TuanDuiBiao.ZhuangTai + "\")";
            }
            return returnstr;
        }



        public t_TuanDuiBiao GetId(int id)
        {
            return _Context.t_TuanDuiBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_TuanDuiBiao> GetList()
        {
            return _Context.t_TuanDuiBiao.Include(x => x.t_TuanDuiRenYuanGuanLianBiaos).Include(x => x.t_RenYuanBiao_JingLi).ToList();
        }
    }
}
